package com.ruoyi.job.task;

import com.alibaba.fastjson.JSONObject;
import com.api.equipment.RemoteEquipmentService;
import com.api.equipment.domain.LgEquipmentError;
import com.api.equipment.domain.LgMachine;
import com.api.equipment.domain.LgTemperature;
import com.api.equipment.domain.LgVehicle;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.job.thread.MacThread;
import com.ruoyi.job.thread.TemThread;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.crypto.Mac;
import java.util.List;
import java.util.Random;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    @Autowired
    private RemoteEquipmentService remoteEquipmentService;
    private static final Random random = new Random();

//    private MqttClient mqttClient;
//
//    String brokerUrl = "tcp://124.221.147.205:1883";
//    String clientId = "temperature_publisher";
//    String topic = "sensor/data";
//    int pubQos = 1;

    MacThread pubMacThread;
    MacThread subMacThread;
    TemThread pubTemThread;
    TemThread subTemThread;

    public void insertOrderLocation()
    {
        remoteEquipmentService.insertOrderLocation();
    }

// 定时任务，每5秒执行一次 startMac()
        public void startMac(){
        System.out.println("startMac");
        List<LgMachine> list = remoteEquipmentService.list(new LgMachine());
        for (LgMachine lgMachine : list){
            if (pubMacThread.getConsoleThreadMap().get("publish" + lgMachine.getId()) != null){
//                pubMacThread.getConsoleThreadMap().get("publish" + lgMachine.getId()).interrupt();
//                subMacThread.getConsoleThreadMap().get("subscribe" + lgMachine.getId()).interrupt();
            }else{
                if (lgMachine.getCurrentStatus() != 2){
                    pubMacThread = new MacThread();
                    pubMacThread.setSerial(String.valueOf(lgMachine.getId()));
                    pubMacThread.setCmd("publish");
                    pubMacThread.start();

                    subMacThread = new MacThread();
                    subMacThread.setSerial(String.valueOf(lgMachine.getId()));
                    subMacThread.setCmd("subscribe");
                    subMacThread.start();
                }
            }

        }
    }

    public void closeMac() {
        List<LgMachine> list = remoteEquipmentService.list(new LgMachine());
        for (LgMachine lgMachine : list) {
            if (pubMacThread.getConsoleThreadMap().get("publish" + lgMachine.getId()) != null) {
                pubMacThread.getConsoleThreadMap().get("publish" + lgMachine.getId()).interrupt();
            }
            if (subMacThread.getConsoleThreadMap().get("subscribe" + lgMachine.getId()) != null) {
                subMacThread.getConsoleThreadMap().get("subscribe" + lgMachine.getId()).interrupt();
            }
        }
    }



    public void startTem(){
//        System.out.println("startMac");
        List<LgTemperature> list = remoteEquipmentService.list1();
        for (LgTemperature lgTemperature : list){
            if (pubMacThread.getConsoleThreadMap().get("publish" + lgTemperature.getId()) != null){
//                pubMacThread.getConsoleThreadMap().get("publish" + lgMachine.getId()).interrupt();
//                subMacThread.getConsoleThreadMap().get("subscribe" + lgMachine.getId()).interrupt();
            }
                if (lgTemperature.getCurrentStatus() != 2){
                    if (!pubTemThread.getConsoleThreadMap().containsKey("publish" + lgTemperature.getId())) {
                        pubTemThread = new TemThread();
                        pubTemThread.setSerial(String.valueOf(lgTemperature.getId()));
                        pubTemThread.setCmd("publish");
                        pubTemThread.start();
                    }
                    if (!subTemThread.getConsoleThreadMap().containsKey("subscribe" + lgTemperature.getId())) {
                        subTemThread = new TemThread();
                        subTemThread.setSerial(String.valueOf(lgTemperature.getId()));
                        subTemThread.setCmd("subscribe");
                        subTemThread.start();
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
//public void publishTemperature() {
//    System.out.println(1111111);
//    }

    public void closeTem() {
        List<LgTemperature> list = remoteEquipmentService.list1();
        for (LgTemperature lgTemperature : list) {
            Thread pubThread = pubTemThread.getConsoleThreadMap().get("publish" + lgTemperature.getId());
            Thread subThread = subTemThread.getConsoleThreadMap().get("subscribe" + lgTemperature.getId());

            if (pubThread != null) {
                pubThread.interrupt();
            } else {
                System.out.println("线程 publish" + lgTemperature.getId() + " 不存在或已停止");
            }

            if (subThread != null) {
                subThread.interrupt();
            } else {
                System.out.println("线程 subscribe" + lgTemperature.getId() + " 不存在或已停止");
            }
        }
    }
}
